package algoritmos;
import java.util.List;

import mapeamento.MapeamentoAbsoluto;
import mapeamento.MapeamentoRelativo;
import mapeamento.Mapeamentos;
import parsedquery.Conceito;
import parsedquery.ParsedQuery;
import finalquery.FinalQuery;
import finalquery.For;


public class DecomposeQuery {
	
	public static String execute(List<Conceito> query, List<MapeamentoAbsoluto> mapAbsoluto, List<MapeamentoRelativo> mapRelativo, String bindedOid, String bindedConcept) {
		FinalQuery finalQuery = new FinalQuery();
		
		boolean binded = false;
		if (bindedConcept != null) {
			binded = true;
		}
		
		System.out.println("\n--- HORIZONTAL FRAGMENTS ----");
		List<For> cFor = HandleHorizontalFragments.execute(query, mapAbsoluto, binded);
		for (int i = 0; i < cFor.size(); i++) {
			System.out.println(cFor.get(i));
		}
		finalQuery.setClausulaFor(cFor);
		
		System.out.println("\n--- NAVIGATE INSIDE FRAGMENTS ----");
		List<For> nFor = NavigateInsideFragments.execute(query, mapRelativo);
		for (int i = 0; i < nFor.size(); i++) {
			System.out.println(nFor.get(i));
		}
		finalQuery.getClausulaFor().addAll(nFor);
		
		System.out.println("\n--- JOIN SPLIT FRAGMENTS ----");
		List<String> where = JoinSplitFragments.execute(query, mapAbsoluto, mapRelativo);
		for (int i = 0; i < where.size(); i++) {
			System.out.println(where.get(i));
		}
		finalQuery.setClausulaWhere(where);
		
		System.out.println("\n--- HANDLE SELECTION PREDICATES ----");
		List<String> whereP = HandleSelectionPredicates.execute(query, mapAbsoluto, mapRelativo);
		for (int i = 0; i < whereP.size(); i++) {
			System.out.println(whereP.get(i));
		}
		finalQuery.getClausulaWhere().addAll(whereP);

		System.out.println("\n--- FINAL QUERY ----");
		finalQuery.setRetorno(query.get(query.size()-1).getOid());
		String retorno = finalQuery.writeFinalQuery(binded);
		return retorno;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<Conceito> query = ParsedQuery.getParsedQuery();
		List<MapeamentoAbsoluto> mapAbsoluto = Mapeamentos.getMapAbsolutos();
		List<MapeamentoRelativo> mapRelativo = Mapeamentos.getMapRelativo();
		FinalQuery finalQuery = new FinalQuery();
		
		System.out.println("--- MAPEAMENTO ABSOLUTO ----");
		for (int i = 0; i < mapAbsoluto.size(); i++) {
			System.out.println(mapAbsoluto.get(i));
		}
		
		System.out.println("\n--- MAPEAMENTO RELATIVO ----");
		for (int i = 0; i < mapRelativo.size(); i++) {
			System.out.println(mapRelativo.get(i));
		}
		
		System.out.println("\n--- PARSED QUERY ----");
		for (int i = 0; i < query.size(); i++) {
			System.out.println(query.get(i));
		}
		
		System.out.println("\n--- HORIZONTAL FRAGMENTS ----");
//		List<For> cFor = HandleHorizontalFragments.execute(query, mapAbsoluto);
//		for (int i = 0; i < cFor.size(); i++) {
//			System.out.println(cFor.get(i));
//		}
//		finalQuery.setClausulaFor(cFor);
		
		System.out.println("\n--- NAVIGATE INSIDE FRAGMENTS ----");
		List<For> nFor = NavigateInsideFragments.execute(query, mapRelativo);
		for (int i = 0; i < nFor.size(); i++) {
			System.out.println(nFor.get(i));
		}
		finalQuery.getClausulaFor().addAll(nFor);
		
		System.out.println("\n--- JOIN SPLIT FRAGMENTS ----");
		List<String> where = JoinSplitFragments.execute(query, mapAbsoluto, mapRelativo);
		for (int i = 0; i < where.size(); i++) {
			System.out.println(where.get(i));
		}
		finalQuery.setClausulaWhere(where);
		
		System.out.println("\n--- FINAL QUERY ----");
		finalQuery.setRetorno(query.get(query.size()-1).getOid());
//		System.out.println(finalQuery.writeFinalQuery());
	}

}
